package com.fh.controller;

import com.fh.common.ResultEnum;
import com.fh.common.ResultMassage;
import com.fh.model.User;
import com.fh.service.UserService;
import com.fh.util.DataTablePageUtil;
import com.fh.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("initqueryUser")
    public String initqueryUser(){
        return "user/user";
    }

    @RequestMapping("queryUser")
    @ResponseBody
    public ResultMassage queryUser(DataTablePageUtil<User> page, UserVo userVo){
        try{
            DataTablePageUtil<User> userDataTablePageUtil = userService.queryUser(page, userVo);
            return new ResultMassage(ResultEnum.QUERY_SUCCESS_MSG,userDataTablePageUtil);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.QUERY_ERROR_MSG);
        }

    }

    @RequestMapping("addUser")
    @ResponseBody
    public ResultMassage addUser(User user){
        try{
            userService.addUser(user);
            return new ResultMassage(ResultEnum.ADD_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.ADD_ERROR_MSG);
        }
    }


    @RequestMapping("toUpdateUser")
    @ResponseBody
    public ResultMassage toUpdateUser(Integer userId){
        try{
            User user = userService.toUpdateUser(userId);
            return new ResultMassage(ResultEnum.TOUPDATE_SUCCESS_MSG,user);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.TOUPDATE_ERROR_MSG);
        }
    }


    @RequestMapping("updateUser")
    @ResponseBody
    public ResultMassage updateUser(User user){
        try{
            userService.updateUser(user);
            return new ResultMassage(ResultEnum.UPDATE_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.UPDATE_SUCCESS_MSG);
        }
    }

    @RequestMapping("deleteUser")
    @ResponseBody
    public ResultMassage deleteUser(Integer userId){
        try{
            userService.deleteUser(userId);
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }
    }

    @RequestMapping("bothdeleteUser")
    @ResponseBody
    public ResultMassage bothdeleteUser(@RequestParam("arr[]") Integer[] arr){
        try{
            userService.deleteBothUser(arr);
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }
    }
}
